
import { Toast } from "antd-mobile";
import { isEmptyString } from "@/utils/utils";
import zoneHttp from "@/utils/zoneRequest";
import { getAreaInfo, setAreaInfo } from "@/utils/storage";
import { setZoneArea } from "./getZoneArea";



/**
 * 
 * 
    // 获取ip
    const fetchIpAddress = async () => {
        try {
            // const response = await axios.get('https://api64.ipify.org?format=json');
            const response = await axios.get('https://ipinfo.io?token=4ef6f29947515a');
            const cip = response.data.ip;
            console.log("=====>ip: " + cip)
            console.log(response.data)
            ZjcUtil.ipInfo = response.data;
            setIpInfo(response.data || {})
        } catch (error) {
            console.error('Error fetching IP address:', error);
        }
    };
 * 
 */

/**
 * 获取地区转发
 * @param {function } reqCallback 响应返回
 * @returns thunk
 */
export const getZone = (reqCallback) => {
    return async dispatch => {
        let areaInfo = getAreaInfo();
        if (areaInfo.length > 0) {
            // 更新状态
            dispatch(setZoneArea(areaInfo));

            //回调
            reqCallback && reqCallback(true, areaInfo)
        } else {
            console.log("getZone: " + process.env.REACT_APP_ZONE);
            let timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

            //组装参数
            let p = process.env.REACT_APP_PRODUCT + "";
            let tz = timeZone + "";
            let url = process.env.REACT_APP_ZONE + "?p=" + p + "&tz=" + tz;
            console.log(url);

            //发起请求
            const params = { p: p, tz: tz }
            const res = await zoneHttp.post(url, params);

            console.log(res);
            const resCode = parseInt(res.code)
            const resObj = res.data
            const resMsg = res.msg
            console.log("获取地区转发-结束：=======")

            //保存
            const success = (resCode === 0)

            //如果有错误，提示
            if (!success) {
                if (!isEmptyString(resMsg)) {
                    Toast.show(resMsg, 1)
                }
            }

            // 切出 path
            let path = "/" + resObj.split(',')[0];
            setAreaInfo(path);
            // 更新状态
            dispatch(setZoneArea(path));

            //回调
            reqCallback && reqCallback(success, path);
        }
    }
}


/**
 * 获取地区转发
 * @param {function } reqCallback 响应返回
 * @returns thunk
 */
export const getZoneRealTime = async (reqCallback) => {

    let areaInfo = getAreaInfo();
    if (areaInfo.length > 0) {
        //回调
        reqCallback && reqCallback(true, areaInfo)

    } else {
        console.log("getZone: " + process.env.REACT_APP_ZONE);
        let timeZone = Intl.DateTimeFormat().resolvedOptions().timeZone;

        //组装参数
        let p = process.env.REACT_APP_PRODUCT + "";
        let tz = timeZone + "";
        let url = process.env.REACT_APP_ZONE + "?p=" + p + "&tz=" + tz;
        console.log(url);

        //发起请求
        const params = { p: p, tz: tz }
        const res = await zoneHttp.post(url, params);

        console.log(res);
        const resCode = parseInt(res.code)
        const resObj = res.data
        const resMsg = res.msg
        console.log("获取地区转发-结束：=======")

        //保存
        const success = (resCode === 0)

        //如果有错误，提示
        if (!success) {
            if (!isEmptyString(resMsg)) {
                Toast.show(resMsg, 1)
            }
        }

        // 切出 path
        let path = "/" + resObj.split(',')[0];
        setAreaInfo(path);

        //回调
        reqCallback && reqCallback(success, path);
    }
}